package com.xujiahong.codegenerator.template;

import cn.hutool.core.date.DateUtil;
import com.xujiahong.codegenerator.Config;
import com.xujiahong.codegenerator.entity.XColumn;
import com.xujiahong.codegenerator.entity.XTable;
import com.xujiahong.codegenerator.tools.ParsecFileTools;

import java.util.Date;
import java.util.List;

public class CreateEntity {

    /**
     * 儿童医院实体
     */
    public static void createETYYEntity(XTable xTable) {
        //装生成代码的buffer对象
        StringBuffer codeBuffer = new StringBuffer();//整个文件的buffer
        StringBuffer fieldBuffer = new StringBuffer();//field

        /*
        代码生成
         */
        codeBuffer.append("package " + Config.ENTITY_PACKAGE + ";\n\n");
        codeBuffer.append("import com.baomidou.mybatisplus.annotation.TableName;\n");
        codeBuffer.append("import "+Config.BASE_PACKAGE+".base.BaseDomain;\n");

        //swagger相关
        if (Config.SWAGGER_ON) {
            codeBuffer.append("import io.swagger.annotations.ApiModel;\n");
            codeBuffer.append("import io.swagger.annotations.ApiModelProperty;\n");
        }

        codeBuffer.append("import lombok.Data;\n");
        codeBuffer.append("import lombok.EqualsAndHashCode;\n");
        if (Config.DATE_TYPE_NAME.equals("LocalDateTime")) {
            codeBuffer.append("import java.time.LocalDateTime;\n");
        } else {
            codeBuffer.append("import java.util.Date;\n");
        }

        codeBuffer.append("\n/**\n" +
                " * Created by AutoGenerateCode on " +
                DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ".\n" +
                " * @author " + Config.AUTHOR + " \n" +
                " */  \n");
        codeBuffer.append("@Data\n");
        codeBuffer.append("@EqualsAndHashCode(callSuper = false)\n");
        codeBuffer.append("@TableName(value = \"" + xTable.getTableName() + "\")\n");

        //swagger相关
        if (Config.SWAGGER_ON) {
            codeBuffer.append("@ApiModel(description = \"" + xTable.getChName() + "\")\n");
        }

        codeBuffer.append("public class " + xTable.getPojoName() + " extends BaseDomain {");

        fieldBuffer.append("\n\n");
        fieldBuffer.append("\tprivate static final long serialVersionUID = 1L;\n\n");

        List<XColumn> columns = xTable.getColumns();
        for (XColumn column : columns) {
            //field

            // 跳过 BaseDomain 中定义的字段
            if (column.getJavaFieldName().equals("id")||
                    column.getJavaFieldName().equals("createTime")||
                    column.getJavaFieldName().equals("updateTime")) {
                continue;
            }

            //swagger相关
            if (Config.SWAGGER_ON) {
                fieldBuffer.append("\t@ApiModelProperty(value = \"" + column.getComment() + "\")\n");
            }
            if ("Date".equals(column.getJavaFieldType())) {
                fieldBuffer.append("\t@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")\n");
                fieldBuffer.append("\t@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")\n");
            }
            fieldBuffer.append("\tprivate " + column.getJavaFieldType() + " " + column.getJavaFieldName() + ";\n\n");
        }

        codeBuffer.append(fieldBuffer);
        codeBuffer.append("}");

        //写入文件
        ParsecFileTools.writeFile(Config.CODE_PATH + "entity/" + xTable.getPojoName() + ".java", codeBuffer);

    }

    /**
     * 创建MybatisPlus实体
     */
    public static void createMybatisPlusEntity(XTable xTable) {
        //装生成代码的buffer对象
        StringBuffer codeBuffer = new StringBuffer();//整个文件的buffer
        StringBuffer fieldBuffer = new StringBuffer();//field

        /*
        代码生成
         */
        codeBuffer.append("package " + Config.ENTITY_PACKAGE + ";\n\n");

        codeBuffer.append("import com.baomidou.mybatisplus.annotation.IdType;\n");
//        codeBuffer.append("import com.baomidou.mybatisplus.annotation.TableField;\n");
        codeBuffer.append("import com.baomidou.mybatisplus.annotation.TableId;\n");
        codeBuffer.append("import com.baomidou.mybatisplus.annotation.TableName;\n");
//        codeBuffer.append("import com.fasterxml.jackson.annotation.JsonFormat;\n");

        //swagger相关
        if (Config.SWAGGER_ON) {
            codeBuffer.append("import io.swagger.annotations.ApiModel;\n");
            codeBuffer.append("import io.swagger.annotations.ApiModelProperty;\n");
        }

        codeBuffer.append("import lombok.Data;\n");
        codeBuffer.append("import lombok.EqualsAndHashCode;\n");
//        codeBuffer.append("import org.springframework.format.annotation.DateTimeFormat;\n");
        if (Config.DATE_TYPE_NAME.equals("LocalDateTime")) {
            codeBuffer.append("import java.time.LocalDateTime;\n");
        } else {
            codeBuffer.append("import java.util.Date;\n");
        }

        codeBuffer.append("\n/**\n" +
                " * Created by AutoGenerateCode on " +
                DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ".\n" +
                " * @author " + Config.AUTHOR + " \n" +
                " */  \n");
        codeBuffer.append("@Data\n");
        codeBuffer.append("@EqualsAndHashCode(callSuper = false)\n");
        codeBuffer.append("@TableName(value = \"" + xTable.getTableName() + "\")\n");

        //swagger相关
        if (Config.SWAGGER_ON) {
            codeBuffer.append("@ApiModel(description = \"" + xTable.getChName() + "\")\n");
        }

        codeBuffer.append("public class " + xTable.getPojoName() + " {");

        fieldBuffer.append("\n\n");

        List<XColumn> columns = xTable.getColumns();
        for (XColumn column : columns) {
            //field
            /**
             * 关键字ID
             */
            if (column.getJavaFieldName().equals("id")) {
                fieldBuffer.append("\t@TableId(value = \"id\", type = IdType.AUTO)\n");
            } else {
//                fieldBuffer.append("\t@TableField(value = \"" + column.getDbFieldName() + "\")\n");
            }
            //swagger相关
            if (Config.SWAGGER_ON) {
                fieldBuffer.append("\t@ApiModelProperty(value = \"" + column.getComment() + "\")\n");
            }
            if ("Date".equals(column.getJavaFieldType())) {
                fieldBuffer.append("\t@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")\n");
                fieldBuffer.append("\t@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")\n");
            }
            fieldBuffer.append("\tprivate " + column.getJavaFieldType() + " " + column.getJavaFieldName() + ";\n\n");
        }

        codeBuffer.append(fieldBuffer);
        codeBuffer.append("}");

        //写入文件
        ParsecFileTools.writeFile(Config.CODE_PATH + "entity/" + xTable.getPojoName() + ".java", codeBuffer);
    }

}
